import pandas as pd
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import tkinter.messagebox
import tkinter
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from save import savepicture1, savepicture2
from matplotlib.animation import FuncAnimation

def showinmap(ID):

    ID_list1 = []
    ID_list2 = []
    f = open('C:\\TEST\\python\\ID_list1.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list1.append(i.strip('\n'))

    f = open('C:\\TEST\\python\\ID_list2.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list2.append(i.strip('\n'))

    if ID not in ID_list1:
        tkinter.messagebox.showerror('警告', '输入的动物ID不存在!')

    elif ID not in ID_list2:
        tkinter.messagebox.showerror('警告', '输入的动物ID数据小于100条！')

    else:
        root1 = tkinter.Tk()
        root1.title('track of animal {}'.format(ID))
        path = 'C:\\TEST\\python\\grouped1\\{}.csv'.format(ID)
        df = pd.read_csv(path)
        lons = list(df["location-long"])
        lats = list(df["location-lat"])
        fig = plt.figure(figsize=(20, 15))  # 设置画布的宽和高
        map = Basemap(llcrnrlon=-10, llcrnrlat=-4, urcrnrlon=59, urcrnrlat=66, resolution='i', area_thresh=5000.0,
                      lat_0=0,
                      lon_0=60)
        # 主要参数分别是左下角的经纬度，右上角的经纬度
        map.drawmapboundary(fill_color='aqua')  # 设置背景色
        map.fillcontinents(color='coral', lake_color='lightskyblue')  # 设置陆地颜色和湖的颜色
        map.drawcoastlines()  # 描出边界
        map.drawcountries(linewidth=0.25)  # 画出国境线
        # 以列表方式储存要描的点的经度，纬度
        x, y = map(lons, lats)
        map.scatter(x, y, marker='o', color='g', zorder=40, s=20)  # 描出多个点的位置，marker为形状，color为颜色，
        # zorder为图册顺序防止被背景色遮挡，s为大小

        canvas = FigureCanvasTkAgg(fig, master=root1)  # A tk.DrawingArea.
        canvas.draw()
        canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
        m = Menu(root1)
        m.add_command(label='保存图片', command=lambda: savepicture1(fig, ID))
        root1['menu'] = m
        root1.mainloop()
        plt.close()
def showoutmap(ID):
    ID_list1 = []
    ID_list2 = []
    f = open('C:\\TEST\\python\\ID_list1.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list1.append(i.strip('\n'))

    f = open('C:\\TEST\\python\\ID_list2.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list2.append(i.strip('\n'))

    if ID not in ID_list1:
        tkinter.messagebox.showerror('警告', '输入的动物ID不存在!')

    elif ID not in ID_list2:
        tkinter.messagebox.showerror('警告', '输入的动物ID数据小于100条！')

    else:
        root1 = tkinter.Tk()
        root1.title('track of animal {}'.format(ID))
        path = 'C:\\TEST\\python\\grouped1\\{}.csv'.format(ID)
        df = pd.read_csv(path)
        lons = list(df["location-long"])
        lats = list(df["location-lat"])
        fig = plt.figure(figsize=(10, 10))
        plt.title('track of animal')
        plt.ylim(-3, 66)  # Ymin和Ymax表示纵轴的最大值和最小值
        plt.xlim(4, 59)  # 同上
        plt.scatter(lons, lats, 1, 'm', marker='o')
        canvas = FigureCanvasTkAgg(fig, master=root1)  # A tk.DrawingArea.
        canvas.draw()
        canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
        m = Menu(root1)
        m.add_command(label='保存图片', command=lambda: savepicture2(fig, ID))
        root1['menu'] = m
        root1.mainloop()
        plt.close()
def move(ID):
    ID_list1 = []
    ID_list2 = []
    f = open('C:\\TEST\\python\\ID_list1.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list1.append(i.strip('\n'))

    f = open('C:\\TEST\\python\\ID_list2.txt', 'r')
    x = f.readlines()
    f.close()
    for i in x:
        ID_list2.append(i.strip('\n'))

    if ID not in ID_list1:
        tkinter.messagebox.showerror('警告', '输入的动物ID不存在!')

    elif ID not in ID_list2:
        tkinter.messagebox.showerror('警告', '输入的动物ID数据小于100条！')

    else:

        path = 'C:\\TEST\\python\\grouped1\\{}.csv'.format(ID)
        df = pd.read_csv(path)
        lons = list(df["location-long"])
        lats = list(df["location-lat"])
        data = [(i, j) for i, j in zip(lons, lats)]
        fig, ax = plt.subplots()
        xdata, ydata = [], []
        ln, = plt.plot([], [], 'g.', markersize=1)

        def init():
            ax.set_xlim(4, 59)
            ax.set_ylim(-3, 66)
            return ln,

        def update(frame):
            xdata.append(frame[0])
            ydata.append(frame[1])
            ln.set_data(xdata, ydata)
            return ln,

        ani = FuncAnimation(fig, update, frames=data,
                            init_func=init, blit=True, interval=1, repeat_delay=3)
        plt.show()
        plt.close()

